package com.tt.common.util;

import java.security.MessageDigest;
import java.util.Random;
import java.util.UUID;

/**
 * 加密相关
 * @author lenovo
 *
 */
public class EncryptedUtil 
{
	/**
	 * 随机生成指定长度长度字符串
	 * @param length 字符串长度
	 * @return
	 */
	public static String getRandomString(int length)
	{ 
		String base = "abcdefghijklmnopqrstuvwxyz0123456789";   
	    Random random = new Random();   
	    StringBuffer sb = new StringBuffer();   
	    for (int i = 0; i < length; i++) 
	    {   
	    	int number = random.nextInt(base.length());   
	        sb.append(base.charAt(number));   
	    }   
	    return sb.toString().toUpperCase();   
	} 
	
	/**
	 * MD5加密字符串
	 * @param str 需要加密的字符串
	 * @param Case 大小写 true 大写 false 小写
	 * @return
	 */
	public static String md5(String str,boolean Case) 
	{
		try 
		{
			MessageDigest md = MessageDigest.getInstance("MD5");
			md.update(str.getBytes());
			byte b[] = md.digest();
			int i;

			StringBuffer buf = new StringBuffer("");
			for (int offset = 0; offset < b.length; offset++) 
			{
				i = b[offset];
				if (i < 0)
				{
					i += 256;
				}
				if (i < 16)
				{
					buf.append("0");
					buf.append(Integer.toHexString(i));
				}
			}
			str = buf.toString();
		} catch (Exception e) 
		{
			e.printStackTrace();
		}
		if(Case)
		{
			return str.toUpperCase();
		}else
		{
			return str.toLowerCase();
		}
	}
	

	/**
	 * 生成32位UUID
	 * @return
	 */
	public static String get32UUID()
	{
		String uuid = UUID.randomUUID().toString().trim().replaceAll("-", "");
		return uuid;
	}
	
	/**
	 *  随机生成六位数验证码 
	 * @return
	 */
	public static int getRandomNum()
	{
		 Random r = new Random();
		 return r.nextInt(900000)+100000;
	}
	
	/**
	 * 产生全球唯一的id
	 * @return
	 */
	public static String generateID()
	{
		return UUID.randomUUID().toString();
	}

}
